"""
用来发送读取数据指令并存储对应的数据
"""
from TestToolsV2.tools.Log import Log
from TestToolsV2.model.Reader import reader

log = Log()


class searcher:
    def __init__(self, dirPath, readType):
        self.dataDict = {}  # 当前路径下的缓存数据，字典结构 {typeNumber：myreader，...}
        self.dirDataDict = {}  # 用来缓存数据，字典结构：{dirPath：{typeNumber：myreader},...}
        self.dirPath = dirPath  # 当前目录
        self.readType = readType
        self.addDir(dirPath)

    def clearDic(self):
        """
        清空缓存中的所有内容\n
        """
        self.dirDataDict.clear()
        self.addDir(self.dirPath)

    def haveDir(self, dirPath):
        """
        判断目录是否在历史列表中\n
        :param dirPath: 目录路径
        :return: 存在返回True
        """
        if not self.dirDataDict:
            return False

        if dirPath in self.dirDataDict.keys():
            return True
        else:
            return False

    def addDir(self, dirPath):
        """
        添加目录到历史列表中,并将目录设置为【当前】目录\n
        :param dirPath: 目录路径
        """
        if not self.haveDir(dirPath):
            self.dirDataDict[dirPath] = {}

        self.setDir(dirPath)

    def setDir(self, dirPath):
        """
        将目录设置为【当前】目录\n
        """
        self.dirPath = dirPath
        self.dataDict = self.dirDataDict[dirPath]

    def haveType(self, dirPath, typeNumber):
        """
        判断EXCEL是否读取过\n
        :param dirPath: 目录路径
        :param typeNumber: 品类编码
        :return: 读取过返回True
        """
        if not self.haveDir(dirPath):
            return False

        if typeNumber in self.dirDataDict[dirPath].keys():
            return True
        else:
            return False

    def addTyepData(self, typeNumber, myreader):
        """
        添加EXCEL读取数据到当【前正】字典缓存\n
        :param typeNumber: key（品类编码）
        :param myreader: valu（EXCEL读取数据对象）
        """
        self.dataDict[typeNumber] = myreader

    def getReader(self, dirPath: str, typeNumber: str):
        """
        获取EXCEL数据\n
        :param dirPath: 目录路径
        :param typeNumber: 品类编码
        :return: 返回EXCEL读取数据对象 myreader
        """
        if typeNumber == '11':
            typeNumber = '10'

        if self.haveType(dirPath, typeNumber):
            return self.dirDataDict[dirPath][typeNumber]
        else:
            return None

    # noinspection PyCompatibility
    def sendOrders(self, typeList):
        """
        通过品类编码列表，读取【多个】EXCEL数据，
        并将读取结果储存在【当前】目录下的缓存字典中\n
        :param typeList: 品类编码列表
        :return: 读取成功返回空字符串，失败返回错误内容
        """
        eInfo = ""
        if not typeList:
            return "erro: 列表为空"

        for typeNumber in typeList:
            isOk = self.doCommand(typeNumber)
            if isOk:
                eInfo = f"{eInfo}\n{typeNumber} : {isOk}"

        if eInfo:
            return eInfo
        else:
            return ""

    # noinspection PyCompatibility
    def doCommand(self, typeNumber, reload=0):
        """
        根据品类，判断是否读取过数据，
        未读取过则读取【单个】EXCEL数据,
        并将读取结果储存【当前】目录下的缓存字典中\n
        :param reload: 是否重载，0表示不重载
        :param typeNumber: 品类类型编码
        :return: 读取成功返回空字符串，失败返回错误内容
        """
        if typeNumber == '11':
            typeNumber = '10'

        if reload == 0:
            if self.haveType(self.dirPath, typeNumber):
                return

        read_type = self.readType.getReadTypeForType(typeNumber)

        if not read_type:
            return

        excelPath = f"{self.dirPath}\\{read_type.NAME}.xlsx"
        _reader = reader()
        result = _reader.ReadBase(excelPath, read_type.INDEX[0], read_type.INDEX[1], True, sheetIndex=0)

        if result:
            self.addTyepData(typeNumber, _reader)
            return ""
        else:
            return _reader.eInfo


"""
def TestSend():
    dirPath = r"E:\LYLTD\doc\程序调用表"
    searcher = searcher()
    isOk = searcher.SendOrders(dirPath, [10, 11, 12, 13, 14])
    if isOk:
        print(isOk)
    else:
        for readers in searcher.dirDict.values():
            for myreader in readers.values():
                print(myreader.headers)


def TestCommand():
    dirPath = r"E:\LYLTD\doc\程序调用表"
    searcher = searcher()
    isOk = searcher.DoCommand(dirPath, 10)
    if isOk:
        print(isOk)
    else:
        print(searcher.dirDict[dirPath][10].headers)


if __name__ == "__main__":
    TestSend()
"""
